
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[VoteAnswers_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteAnswers_Insert]

IF OBJECT_ID(N'[dbo].[VoteAnswers_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteAnswers_Update]

IF OBJECT_ID(N'[dbo].[VoteAnswers_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteAnswers_Delete]

IF OBJECT_ID(N'[dbo].[VoteAnswers_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[VoteAnswers_Select]

--endregion

GO


--region [dbo].[VoteAnswers_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteAnswers_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdVoteAnswer int = null,
	@VoteAnswerContent nvarchar(1000) = null,
	@IsActived bit = null,
	@CreatedDate datetime = null,
	@LastModifiedDate datetime = null,
	@CountVote int = null,
	@IdVoteQuestion int = null,
	@SortOrder int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[VoteAnswers]
					WHERE
					(
						(@IdVoteAnswer is null OR [VoteAnswers].[IdVoteAnswer] = @IdVoteAnswer)
						AND (@VoteAnswerContent is null OR [VoteAnswers].[VoteAnswerContent] like @VoteAnswerContent)
						AND (@IsActived is null OR [VoteAnswers].[IsActived] = @IsActived)
						AND (@CreatedDate is null OR [VoteAnswers].[CreatedDate] = @CreatedDate)
						AND (@LastModifiedDate is null OR [VoteAnswers].[LastModifiedDate] = @LastModifiedDate)
						AND (@CountVote is null OR [VoteAnswers].[CountVote] = @CountVote)
						AND (@IdVoteQuestion is null OR [VoteAnswers].[IdVoteQuestion] = @IdVoteQuestion)
						AND (@SortOrder is null OR [VoteAnswers].[SortOrder] = @SortOrder)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[VoteAnswers].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdVoteAnswer' and @SortType = 1 THEN [VoteAnswers].[IdVoteAnswer] END ASC,
						CASE WHEN @SortBy = 'IdVoteAnswer' and @SortType = 0 THEN [VoteAnswers].[IdVoteAnswer] END DESC,
						CASE WHEN @SortBy = 'VoteAnswerContent' and @SortType = 1 THEN [VoteAnswers].[VoteAnswerContent] END ASC,
						CASE WHEN @SortBy = 'VoteAnswerContent' and @SortType = 0 THEN [VoteAnswers].[VoteAnswerContent] END DESC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 1 THEN [VoteAnswers].[IsActived] END ASC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 0 THEN [VoteAnswers].[IsActived] END DESC,
						CASE WHEN @SortBy = 'CreatedDate' and @SortType = 1 THEN [VoteAnswers].[CreatedDate] END ASC,
						CASE WHEN @SortBy = 'CreatedDate' and @SortType = 0 THEN [VoteAnswers].[CreatedDate] END DESC,
						CASE WHEN @SortBy = 'LastModifiedDate' and @SortType = 1 THEN [VoteAnswers].[LastModifiedDate] END ASC,
						CASE WHEN @SortBy = 'LastModifiedDate' and @SortType = 0 THEN [VoteAnswers].[LastModifiedDate] END DESC,
						CASE WHEN @SortBy = 'CountVote' and @SortType = 1 THEN [VoteAnswers].[CountVote] END ASC,
						CASE WHEN @SortBy = 'CountVote' and @SortType = 0 THEN [VoteAnswers].[CountVote] END DESC,
						CASE WHEN @SortBy = 'IdVoteQuestion' and @SortType = 1 THEN [VoteAnswers].[IdVoteQuestion] END ASC,
						CASE WHEN @SortBy = 'IdVoteQuestion' and @SortType = 0 THEN [VoteAnswers].[IdVoteQuestion] END DESC,
						CASE WHEN @SortBy = 'SortOrder' and @SortType = 1 THEN [VoteAnswers].[SortOrder] END ASC,
						CASE WHEN @SortBy = 'SortOrder' and @SortType = 0 THEN [VoteAnswers].[SortOrder] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[VoteAnswers]
			Where 
			(
				(@IdVoteAnswer is null OR [VoteAnswers].[IdVoteAnswer] = @IdVoteAnswer)
				AND (@VoteAnswerContent is null OR [VoteAnswers].[VoteAnswerContent] like @VoteAnswerContent)
				AND (@IsActived is null OR [VoteAnswers].[IsActived] = @IsActived)
				AND (@CreatedDate is null OR [VoteAnswers].[CreatedDate] = @CreatedDate)
				AND (@LastModifiedDate is null OR [VoteAnswers].[LastModifiedDate] = @LastModifiedDate)
				AND (@CountVote is null OR [VoteAnswers].[CountVote] = @CountVote)
				AND (@IdVoteQuestion is null OR [VoteAnswers].[IdVoteQuestion] = @IdVoteQuestion)
				AND (@SortOrder is null OR [VoteAnswers].[SortOrder] = @SortOrder)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[VoteAnswers_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[VoteAnswers_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteAnswers_Insert]
	@IdVoteAnswer int OUTPUT,
	@VoteAnswerContent nvarchar(1000),
	@IsActived bit,
	@CreatedDate datetime,
	@LastModifiedDate datetime,
	@CountVote int,
	@IdVoteQuestion int,
	@SortOrder int

AS


INSERT INTO [dbo].[VoteAnswers] 
(
	[VoteAnswerContent],
	[IsActived],
	[CreatedDate],
	[LastModifiedDate],
	[CountVote],
	[IdVoteQuestion],
	[SortOrder]
)
VALUES 
(
	@VoteAnswerContent,
	@IsActived,
	@CreatedDate,
	@LastModifiedDate,
	@CountVote,
	@IdVoteQuestion,
	@SortOrder
)

SET @IdVoteAnswer = SCOPE_IDENTITY()

--end [dbo].[VoteAnswers_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[VoteAnswers_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteAnswers_Update]
	@IdVoteAnswer int,
	@VoteAnswerContent nvarchar(1000),
	@IsActived bit,
	@CreatedDate datetime,
	@LastModifiedDate datetime,
	@CountVote int,
	@IdVoteQuestion int,
	@SortOrder int
AS


UPDATE [dbo].[VoteAnswers] SET
	[VoteAnswerContent] = @VoteAnswerContent,
	[IsActived] = @IsActived,
	[CreatedDate] = @CreatedDate,
	[LastModifiedDate] = @LastModifiedDate,
	[CountVote] = @CountVote,
	[IdVoteQuestion] = @IdVoteQuestion,
	[SortOrder] = @SortOrder
WHERE
	[IdVoteAnswer] = @IdVoteAnswer

--end [dbo].[VoteAnswers_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[VoteAnswers_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[VoteAnswers_Delete]
	@IdVoteAnswer int
AS


DELETE FROM [dbo].[VoteAnswers]
WHERE
(
	[IdVoteAnswer] = @IdVoteAnswer
)

--end [dbo].[VoteAnswers_Delete]
--endregion

GO
--=========================================================================================--

